<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>依赖注入</title>
    <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>

</head>
<body>
    <div id="app">
        <p>我是根元素</p>
        <my-one></my-one>
    </div>    
    <script>
        Vue.component("my-one",{
            inject:["msg","getStr"],
            // 访问
            template:`
            <div style="border:1px solid red">
                <p>{{msg}}</p>
                <p>{{getStr()}}</p>
            </div>
            `,
           
        });
        var app = new Vue({
            el:"#app",
            data:{
                msg:'依赖提供的数据',
            },
            methods: {
                getStr(){
                    console.log("依赖提供的方法");
                    return "依赖方法返回的值"
                }
            },

            provide(){
                return {
                    msg:this.msg,
                    getStr:this.getStr,
                }    
            }



           
        })
    </script>
</body>
</html>